package com.vanzo.some.interphone;

import android.content.Context;
import android.content.Intent;
import android.content.res.XmlResourceParser;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.Toast;

import com.vanzo.some.R;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tisen on 2017/12/29.
 */

public class ConnectHandle {

    private static final String TAG = "ConnectHandle";
    private static List<Connect> connects;

    public static void init(Context context) {
        initConnects(context);
    }

    private static List<Connect> initConnectFromXml(Context context) {
        Log.e(TAG, "init Connects from xml start");
        ConnectParser parser = new ConnectParser();
        XmlResourceParser xmlResourceParser = context.getResources().getXml(R.xml.default_connects);
        try {
            connects = parser.parse(xmlResourceParser);
        } catch (Exception e) {
            Log.e(TAG, "init Connects error");
            e.printStackTrace();
        }

        return connects;
    }

    private static List<Connect> initConnectFromDB(Context context) {
        Log.e(TAG, "init Connects from db start");
        return new ArrayList<Connect>();
    }

    private static void initConnects(Context context) {
        if (context.getSharedPreferences(InterApplication.SHARED_PREFERENCES_NAME, InterApplication.SHARED_PREFERENCES_MODE)
                .getBoolean("hasXmlInit", false)) {
            connects = initConnectFromDB(context);
        } else {
            connects = initConnectFromXml(context);
        }
    }

    public static List<Connect> getConnects(Context context) {
        if (connects == null)
            initConnects(context);
        return connects;
    }

    public static void outConnectsToFile(Context context) {
        if (connects != null) {
            ConnectParser parser = new ConnectParser();
            try {
                String xmlString = parser.serialize(connects);
                String fileName = "/sdcard/" + InterApplication.DEFAULT_CONNECTS_FILE_NAME;
                FileOutputStream outStream = new FileOutputStream(fileName, false);
                OutputStreamWriter writer = new OutputStreamWriter(outStream, "utf-8");
                writer.write(xmlString.toString());
                writer.flush();
                writer.close();//记得关闭

                outStream.close();
                Toast.makeText(context, "导出至" + fileName, Toast.LENGTH_LONG).show();
                Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                scanIntent.setData(Uri.fromFile(new File(fileName)));
                context.sendBroadcast(scanIntent);
            } catch (Exception e) {
                Log.d(TAG, "out Connect to xml error");
                e.printStackTrace();
            }
        }
    }

    public static void addConnect(@NonNull Connect connect) {
        if (connect == null) {
            Log.d(TAG, "addConnect, connect_item is null");
            return;
        }
        if (connects == null) {
            Log.d(TAG, "addConnect, connects is null,now init");
            connects = new ArrayList<>();
        }
        if (connects.contains(connect)) {
            connects.remove(connect);
        }
        connects.add(connect);
    }

    public static void updateConnect(int position, @NonNull Connect connect) {
        if (connect == null) {
            Log.d(TAG, "updateConnect, connect_item is null");
            return;
        }
        if (connects == null) {
            Log.d(TAG, "updateConnect, connects is null,now init");
            connects = new ArrayList<>();
        }
        if (position < 0 || position >= connects.size()) {
            Log.d(TAG, "updateConnect, position is out of size");
            return;
        } else {
            connects.remove(position);
        }
        connects.add(position, connect);
    }
}
